package com.tledu.production.controller.people;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.constant.CommonConstant;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.people.Employee;
import com.tledu.production.service.people.IEmployeeService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Antg
 * @date 2021/8/17  11:19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/people/emp")
public class EmployeeController {
    private final IEmployeeService employeeService;

    @GetMapping("/list")
    public AjaxResult<PageInfo<Employee>> findAll(Integer pageNum, Integer pageSize) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeService.findAll();
        PageInfo<Employee> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    @GetMapping("/search")
    public AjaxResult<PageInfo<Employee>> search(Integer pageNum, Integer pageSize, String type, String searchText) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageSize = CommonConstant.PAGESIZE;
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = employeeService.search(type, searchText);
        PageInfo<Employee> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    @PostMapping("/add")
    public AjaxResult add(Employee employee) {
        int res = employeeService.add(employee);
        if (res == 1) {
            return new AjaxResult("成功");
        } else {
            return new AjaxResult("失败", 500);
        }
    }

    @PostMapping("/edit")
    public AjaxResult edit(Employee employee) {
        int res = employeeService.edit(employee);
        if (res == 1) {
            return new AjaxResult("编辑成功");
        } else {
            return new AjaxResult("编辑失败", 500);
        }
    }

    @GetMapping("/del")
    public AjaxResult batchDel(String ids) {
        int res = employeeService.batchDel(ids.split(","));
        if (res >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }

}
